package com.zhangh.summer.admin.support.ds.mysql;



import com.zhangh.summer.admin.support.ds.mysql.mapper.BizMapper;
import com.zhangh.summer.admin.support.ds.mysql.mapper.TableMapper;
import com.zhangh.summer.admin.support.ds.mysql.mapper.TableShardMapper;
import com.zhangh.summer.admin.support.ds.mysql.mapper.BizShardMapper;
import com.zhangh.summer.admin.support.multipleds.DynamicDataSource;
import com.zhangh.summer.admin.support.spring.util.SpringBeanUtil;

import java.util.HashMap;
import java.util.Map;

public class MybatisMapperFactory implements IMybatisMapperFactory {
    private static MybatisMapperFactory singleton = null;
    private static final Map<String, BizMapper> BIZ_MAPPER_MAP=new HashMap<>();
    private static final Map<String, TableShardMapper> TABLE_SPLIT_MAPPER_MAP=new HashMap<>();
    private static final Map<String, BizShardMapper> BIZ_SPLIT_MAPPER_MAP=new HashMap<>();
    //private static final DataSourceSupport dataSourceSupport = (DataSourceSupport) SpringBeanUtil.getBeanByType(DataSourceSupport.class);
    private DynamicDataSource dynamicDataSource= SpringBeanUtil.getBeanByType(DynamicDataSource.class);

    public static MybatisMapperFactory getInstance(){
        if(singleton == null){
            synchronized (MybatisMapperFactory.class){
                if(singleton == null){
                    singleton = new MybatisMapperFactory();
                }
            }
        }
        return singleton;
    }
    /**
     * 构建无分表自定义sql mapper
     * @param daoName  dao名称（除去namespace的名称）
     * @return
     */
    public BizMapper mapper(String daoName){
        String namespace= "mybatis.mapper.biz."+daoName;//SysConstances.CFG_MAP.get("business.namespace")+"."+daoName;
        BizMapper dao=BIZ_MAPPER_MAP.get(namespace);
        if (null==dao){
            synchronized (this){
                dao=new BizMapper(namespace);
                BIZ_MAPPER_MAP.put(namespace,dao);
            }
        }
        return dao;
    }

    /**
     * 构建 mybatis标准mapper
     * @return
     */
    public TableMapper mapper(){
        return TableMapper.getInstance();
    }

    /**
     * 构建分表 mybatis标准mapper
     * @param tableShardKey
     * @return
     */
    public TableShardMapper mapper(long tableShardKey){
        TableShardMapper dao=TABLE_SPLIT_MAPPER_MAP.get(String.valueOf(tableShardKey));
        if (null==dao){
            synchronized (this){
                dao= new TableShardMapper(tableShardKey);
                TABLE_SPLIT_MAPPER_MAP.put(String.valueOf(tableShardKey),dao);
            }
        }
        return dao;
    }

    /**
     * 构建含有分表自定义sql mapper
     * @param tableShardKey 分表key
     * @param daoName dao名称（除去namespace的名称）
     * @return
     */
    public BizShardMapper mapper(long tableShardKey, String daoName){
        String namespace="mybatis.mapper.biz."+daoName;//SysConstances.CFG_MAP.get("business.namespace")+"."+daoName;
        String key=namespace+tableShardKey;

        BizShardMapper dao=BIZ_SPLIT_MAPPER_MAP.get(key);
        if (null==dao){
            synchronized (this){
                dao=new BizShardMapper(namespace,tableShardKey);
                BIZ_SPLIT_MAPPER_MAP.put(key,dao);
            }
        }
        return dao;
    }

    /*@Override
    public Connection conn() throws Exception {
        return dynamicDataSource.getConnection();
    }*/

}
